package com.enjoy.studyfragment.util;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.Gravity;

import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.bean.BuildBean;
import com.dou361.dialogui.bean.TieBean;
import com.dou361.dialogui.listener.DialogUIItemListener;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

/**
 * 拍照或相册选择工具类
 * Created by Administrator on 2018/3/16.
 */

public class PictureDialogUtil {

    public static BuildBean initDialog(Activity activity,final Fragment fragment, final List<LocalMedia> selectList){
        List<TieBean> list = new ArrayList<TieBean>();
        list.add(new TieBean("拍照"));
        list.add(new TieBean("相册"));
        BuildBean dialog = DialogUIUtils.showSheet(activity, list, "取消", Gravity.BOTTOM, true, true, new DialogUIItemListener() {
            @Override
            public void onItemClick(CharSequence text, int position) {
                itemClickPicture(position,fragment,selectList);
            }

            @Override
            public void onBottomBtnClick() {
                DialogUIUtils.showToast("取消");
            }
        });
        return dialog;
    }

    /**
     * 拍照、相册
     * @param position
     */
    public static void itemClickPicture(int position,Fragment fragment,List<LocalMedia> selectList){
        if(0 == position){ //拍照
            PictureSelector.create(fragment)
                    .openCamera(PictureMimeType.ofImage())
                    .maxSelectNum(9)
                    .minSelectNum(1)
                    .glideOverride(160, 160)
                    .previewEggs(true)
                    .previewImage(true)
                    .selectionMedia(selectList)
                    .compress(true)
                    .forResult(PictureConfig.CHOOSE_REQUEST);
        }
        if(1 == position){ //相册
            PictureSelector.create(fragment)
                    .openGallery(PictureMimeType.ofImage())
                    .maxSelectNum(9)
                    .minSelectNum(1)
                    .glideOverride(160, 160)
                    .previewEggs(true)
                    .previewImage(true)
                    .isCamera(false)
                    .selectionMedia(selectList)
                    .compress(true)
                    .forResult(PictureConfig.CHOOSE_REQUEST);
        }
    }

    public static void showPicture(Activity activity,Fragment fragment, List<LocalMedia> selectList){
        initDialog(activity,fragment,selectList).show();
    }

}
